package com.ihrm.system.controller;

import com.ihrm.common.entity.PageResult;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;

import com.ihrm.domain.system.User;
import com.ihrm.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

//1..解决跨域
@CrossOrigin
//2.设置restController
@RestController
//3.设置父路径
@RequestMapping(value = "/sys")
public class UserControlller {
    @Autowired
    UserService userService;

    /**
     * 保存
     * @return
     */
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public Result save(@RequestBody User user){
        //1.设置保存的企业IID
        String companyId="1";
        user.setCompanyId(companyId);
        //user.setCompanyName(companyName);
        //2.调用service完成保存企业
        userService.save(user);
        //3.构造返回结果
        return new Result(ResultCode.SUCCESS);
    }
    //查询企业的部门列表
    //指定企业id
    @RequestMapping(value = "/user",method = RequestMethod.GET)
    public Result  findAll(int page, int size, @RequestParam()Map map){
        //1.指定企业id
        String companyId="1";
        map.put("companyId",companyId);
        //2.完成查询
        Page<User> pageUsr= userService.findAll(map,page,size);
        //3.构造返回及结果
        PageResult<User> pageResult=new PageResult(pageUsr.getTotalElements(),pageUsr.getContent());

        return new Result(ResultCode.SUCCESS,pageResult);

    }
    //根据id查询user
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public Result  findById(@PathVariable(value = "id") String id){
        User user = userService.findById(id);
        return new Result(ResultCode.SUCCESS,user);
    }
    //修改user
    @RequestMapping(value = "/user/{id}",method = RequestMethod.PUT)
    public  Result  update(@PathVariable(value = "id")String id,@RequestBody User user){
        //设置修改的部门 ID
        user.setId(id);
        //调用service进行更新

        userService.update(user);
        return new Result(ResultCode.SUCCESS);
    }
    //根据ID进行删除
    @RequestMapping(value = "/user/{id}",method = RequestMethod.DELETE)
    public  Result  delete(@PathVariable(value = "id")String id){
        userService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }
}

